﻿var paging = function ($) {

    function formatString(text) {
        if (arguments.length <= 1) {
            return text;
        }
        var tokenCount = arguments.length - 2;
        for (var token = 0; token <= tokenCount; token++) {
            text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
        }
        return text;
    }

    function execCallback(callback) {
        if (typeof callback == "function")
            return callback;
        return false;
    }

    function generate(pageIndex, sumPage, url, contentPlaceHolder, callback) {
        var html = '';
        var format = "<a href='{0}' title='trang {1}'>{1}</a>";
        for (var i = 0; i < sumPage; i++) {
            html += formatString(format, formatString('{0}&page={1}', url, i), i);
        }
        $(contentPlaceHolder).html(html);
        execCallback(callback);
    }

    return {
        generate: function (pageIndex, sumPage, url, contentPlaceHolder, callback) {
            return generate(pageIndex, sumPage, url, contentPlaceHolder);
        }
    };
} (jQuery);